
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _utils = require('../utils');

const _constant = require('../utils/constant');

const _string = require('../utils/format/string');

const _checkbox = _interopRequireDefault(require('../checkbox'));

const _createNamespace = (0, _utils.createNamespace)('coupon');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];
const t = _createNamespace[2];

function getDate(timeStamp) {
  const date = new Date(timeStamp * 1000);
  return `${date.getFullYear()}.${(0, _string.padZero)(date.getMonth() + 1)}.${(0, _string.padZero)(date.getDate())}`;
}

function formatDiscount(discount) {
  return (discount / 10).toFixed(discount % 10 === 0 ? 0 : 1);
}

function formatAmount(amount) {
  return (amount / 100).toFixed(amount % 100 === 0 ? 0 : amount % 10 === 0 ? 1 : 2);
}

const _default = createComponent({
  props: {
    coupon: Object,
    chosen: Boolean,
    disabled: Boolean,
    currency: {
      type: String,
      default: '¥',
    },
  },
  computed: {
    validPeriod: function validPeriod() {
      const _this$coupon = this.coupon;
      const { startAt } = _this$coupon;
      const { endAt } = _this$coupon;
      return `${getDate(startAt)} - ${getDate(endAt)}`;
    },
    faceAmount: function faceAmount() {
      const { coupon } = this;

      if (coupon.valueDesc) {
        return `${coupon.valueDesc}<span>${coupon.unitDesc || ''}</span>`;
      }

      if (coupon.denominations) {
        const denominations = formatAmount(coupon.denominations);
        return `<span>${this.currency}</span> ${denominations}`;
      }

      if (coupon.discount) {
        return t('discount', formatDiscount(coupon.discount));
      }

      return '';
    },
    conditionMessage: function conditionMessage() {
      const condition = formatAmount(this.coupon.originCondition);
      return condition === '0' ? t('unlimited') : t('condition', condition);
    },
  },
  render: function render() {
    const h = arguments[0];
    const { coupon } = this;
    const { disabled } = this;
    const description = disabled && coupon.reason || coupon.description;
    return h('div', {
      class: bem({
        disabled,
      }),
    }, [h('div', {
      class: bem('content'),
    }, [h('div', {
      class: bem('head'),
    }, [h('h2', {
      class: bem('amount'),
      domProps: {
        innerHTML: this.faceAmount,
      },
    }), h('p', {
      class: bem('condition'),
    }, [this.coupon.condition || this.conditionMessage])]), h('div', {
      class: bem('body'),
    }, [h('p', {
      class: bem('name'),
    }, [coupon.name]), h('p', {
      class: bem('valid'),
    }, [this.validPeriod]), !this.disabled && h(_checkbox.default, {
      attrs: {
        size: 18,
        value: this.chosen,
        checkedColor: _constant.RED,
      },
      class: bem('corner'),
    })])]), description && h('p', {
      class: bem('description'),
    }, [description])]);
  },
});

exports.default = _default;
